﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.VehicleReport.BLL;
using Yipusoft.VehicleReport.Model;
using Yipusoft.BasicBase.Model;
using Yipusoft.BasicBase.BLL;
using Yipusoft.Common.BLL;

public partial class DutyLog_VehicleReportView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }

    }
    string GetDeviceCountUnit(MendDeviceInfo menddevice)
    {
        string strCount = menddevice.Count.ToString();
        DeviceInfo device = Device.GetDevice(menddevice.DeviceId);
        if (device != null)
        {
            Yipusoft.Common.Model.CodeItemInfo iteminfo = CodeItem.GetCodeItem(device.UnitName);
            string UnitName = "";

            if (iteminfo != null)
            {
                UnitName = iteminfo.ItemName;

            }
            string[] strCounts = strCount.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);

            if (strCounts.Count() > 1)
            {
                if (Convert.ToInt32(strCounts[1]) > 0)
                {
                    strCount = strCount + UnitName;
                }
                else
                {
                    strCount = strCounts[0] + UnitName;
                }
            }
            else
            {
                strCount = strCounts[0] + UnitName;
            }

            return strCount;
        }
        return strCount;
    }

    void BindData()
    {
        DateTime date = Convert.ToDateTime(Request["date"]);
        string DepartmentId = Request["DepartmentId"];
        DAL.Account_Department department = BLL.Department.GetDepartmentByID(DepartmentId);
        tb.Caption = "<span><b>" + department.ShortName + "【" + date.ToString("yyyy年MM月dd日") + "】车辆装备检查<b/></span>";


        //头
        TableRow headTr = new TableRow();
        TableCell cell = new TableCell();
        cell.Text = "车辆";

        headTr.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "情况说明";

        headTr.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "支队确认";
        cell.Style.Add("width", "80px");
        headTr.Cells.Add(cell);


        cell = new TableCell();
        cell.Text = "大队确认";
        cell.Style.Add("width", "80px");
        headTr.Cells.Add(cell);

        cell = new TableCell();
        cell.Text = "中队确认";
        cell.Style.Add("width", "80px");
        headTr.Cells.Add(cell);

        tb.Rows.Add(headTr);

        string where = " DepartmentId='" + DepartmentId + "' and  DayDate='" + date.Date + "' ";

        List<VehicleDayItemInfo> list = VehicleDayItem.GetList(where);

        for (int i = 0; i < list.Count; i++)
        {

            int ros = 0;
            //车辆
            TableRow tr = new TableRow();
            TableCell headTd = new TableCell();
            headTd.Text = list[i].VehicleName + "<br/>【" + list[i].NumberPlate + "】";
            headTd.Style.Add("width", "120px");
            tr.Cells.Add(headTd);

            TableCell td;



            //支队确认

            string mendWhere = " MendTime<='" + date.AddDays(1) + "' and finishTime>='" + date.AddDays(1) + "' and DepartmentId='" + DepartmentId + "' and VehicleID='" + list[i].VehicleID + "' order by mendTime Desc";
            MendRecordInfo record = MendRecord.GetList(mendWhere).FirstOrDefault();

            if (record != null)
            {
                td = new TableCell();
                if (record.MendTime.Date < date)
                {
                    TimeSpan timeLong = date - record.MendTime.Date;
                    int DayCount = timeLong.Days;
                    td.Text = record.Remark + "<br/><div style='color:Red;'>上报时间为：" + record.MendTime.ToString("yyyy-MM-dd") + "，距今已" + DayCount + "天。</div>";
                }
                else
                {
                    td.Text = list[i].Remark;
                }

                tr.Cells.Add(td);

                td = new TableCell();
                td.Text = record.ZhiDOK;
                tr.Cells.Add(td);

                td = new TableCell();


                td.Text = record.DZDOk;

                tr.Cells.Add(td);

                td = new TableCell();

                if (record.ZhongdOk.Trim() != "")
                {
                    td.Text = record.ZhongdOk + "<br>" + record.FinishTime.ToString("yyyy-MM-dd");
                }
                else
                {
                    td.Text = "";
                }


                tr.Cells.Add(td);

                tb.Rows.Add(tr);
                ros++;
            }
            //器材
            string DeviceWhere = " MendTime<='" + date.AddDays(1) + "' and finishTime>='" + date.AddDays(1) + "' and DepartmentId='" + DepartmentId + "' and Vid='" + list[i].VehicleID + "' order by mendTime Desc";
            List<MendDeviceInfo> devices = MendDevice.GetList(DeviceWhere);

            for (int j = 0; j < devices.Count; j++)
            {
                if (ros != 0)
                {
                    tr = new TableRow();
                }

                ros++;
                td = new TableCell();


                td.Text = devices[j].DeviceName;

                td.Text += "&nbsp;&nbsp;&nbsp;&nbsp;" + GetDeviceCountUnit(devices[j]);

                td.Text += "&nbsp;&nbsp;&nbsp;&nbsp;" + devices[j].Remark + "<br/>" + devices[j].DealNotes;
                if (devices[j].MendTime.Date < date)
                {
                    TimeSpan timeLong = date - devices[j].MendTime.Date;
                    int DayCount = timeLong.Days;
                    td.Text += "<br/><div style='color:Red;'>上报时间为：" + devices[j].MendTime.ToString("yyyy-MM-dd") + "，距今已" + DayCount + "天。</div>";
                }
                tr.Cells.Add(td);

                td = new TableCell();
                td.Text = devices[j].ZhidOk;
                tr.Cells.Add(td);
                td = new TableCell();
                td.Text = devices[j].DadOk;
                tr.Cells.Add(td);
                td = new TableCell();
                if (devices[j].ZhongdOk != "")
                {
                    td.Text = devices[j].ZhongdOk + "<br/>" + devices[j].FinishTime.ToString("yyyy-MM-dd");
                }
                else
                {
                    td.Text = "";
                }

                tr.Cells.Add(td);

                tb.Rows.Add(tr);
            }
            if (record == null && devices.Count == 0)
            {
                td = new TableCell();
                td.Text = "";

                tr.Cells.Add(td);

                td = new TableCell();
                td.Text = "";
                tr.Cells.Add(td);

                td = new TableCell();
                td.Text = "";
                tr.Cells.Add(td);

                td = new TableCell();
                td.Text = "";
                tr.Cells.Add(td);

                tb.Rows.Add(tr);
            }
            headTd.RowSpan = ros;

        }


    }
}
